﻿using System;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using InstrumentCheck.Module;
using InstrumentCheck.BLL;
using System.Net;
using System.Net.Mail;
using System.Net.Configuration;
using System.Security.Principal;

namespace InstrumentCheck.WebUI
{
    public class PublicFunction
    {
        public PublicFunction()
        { }

        public static string strAttachPath
        {
            get
            {
                return System.Threading.Thread.GetDomain().BaseDirectory + ConfigurationManager.AppSettings["AttachPath"].ToString();
            }
        }
        //public static string strAttachPath = System.Configuration.ConfigurationManager.AppSettings["AttachPath"].ToString();

        public static bool CheckUser(System.Web.UI.Page page)
        {
            try
            {
                WindowsPrincipal user = new WindowsPrincipal(WindowsIdentity.GetCurrent());
                string domainName = user.Identity.Name;
                string[] ary = domainName.Split('\\');
                string name = ary[ary.Length - 1].ToString();
                User LoginUser = User_bll.GetUserInfoByName_bll(name);
                if (LoginUser != null)
                {
                    page.Session["UserInfo"] = LoginUser;
                    return true;
                }
                else
                    return false;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static string Decode(string str)
        {
            return System.Web.HttpUtility.HtmlDecode(str);
        }

        public static void SendMail(MailAddress mailForm, MailAddress[] mailTo, MailAddress[] mailCC, string subject, string mailBody, params string[] attachmentPath)
        {
            MailMessage myMail = new MailMessage();
            System.Text.UTF8Encoding mailEncoding = new System.Text.UTF8Encoding();
            if (mailForm != null)
                myMail.From = mailForm;
            else
                myMail.From = new MailAddress("eformadmin@aopen.com");

            if (mailTo.Length > 0)
            {
                foreach (MailAddress mailAddress in mailTo)
                {
                    myMail.To.Add(mailAddress);
                }
            }

            if (mailCC != null)
            {
                foreach (MailAddress mailAddCC in mailCC)
                {
                    myMail.CC.Add(mailAddCC);
                }
            }
            myMail.Bcc.Add(new MailAddress("peterchen@aopen.com"));

            myMail.Subject = subject;
            myMail.BodyEncoding = mailEncoding;
            myMail.IsBodyHtml = true;
            myMail.Body = mailBody;

            if (attachmentPath != null && attachmentPath.Length > 0)
            {
                foreach (string attachment in attachmentPath)
                {
                    myMail.Attachments.Add(new Attachment(attachment));
                }
            }

            SmtpClient smtp = new SmtpClient("aoims01");
            smtp.Send(myMail);
        } 
    }
}
